package cevent.source.cloudcenter.server.service;

import cevent.source.cloudcenter.server.domain.SiteSingle;
import cevent.source.cloudcenter.server.domain.SiteSingleExample;
import cevent.source.cloudcenter.server.dto.PageDto;
import cevent.source.cloudcenter.server.dto.SiteSingleDto;
import cevent.source.cloudcenter.server.mapper.SiteSingleMapper;
import cevent.source.cloudcenter.server.util.DuplicateUtil;
import cevent.source.cloudcenter.server.util.UUID8Util;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author cevent
 * @description 独立信息设置表服务层
 */
@Service
public class SiteSingleService {
    private static final Logger LOG=LoggerFactory.getLogger(SiteSingleService.class);
    @Resource
    private SiteSingleMapper siteSingleMapper;

    //1.无分页列表查询：dto获取domain参数
    public List<SiteSingleDto> siteSingleDtoList(){
        SiteSingleExample siteSingleExample = new SiteSingleExample();
        siteSingleExample.setOrderByClause("uni_id asc");
        List<SiteSingle> siteSingleList=siteSingleMapper.selectByExampleWithBLOBs(siteSingleExample);
        List<SiteSingleDto> siteSingleDtoList = new ArrayList<SiteSingleDto>();
        for(int i=0;i<siteSingleList.size();i++){
            SiteSingle siteSingle=siteSingleList.get(i);
            SiteSingleDto siteSingleDto=new SiteSingleDto();
            //复制拷贝(source,target)
            BeanUtils.copyProperties(siteSingle,siteSingleDto);
            siteSingleDtoList.add(siteSingleDto);
        }
        return siteSingleDtoList;
    }

    /*2.分页查询：pageDto保存数据
        list数据集合：listPageData
        当前页码：currentPage
        每页行数：size
        总计函数：totalSize
        前端可返回pageDto，无需返回值
     */
    public void getSiteSinglePageDtoList(PageDto pageDto){
        //1.分页设置（前端传入当前页和每页行数params），起始页，每页行数size
        PageHelper.startPage(pageDto.getCurrentPage(),pageDto.getSize());

        SiteSingleExample siteSingleExample=new SiteSingleExample();


        List<SiteSingle> siteSingleList=siteSingleMapper.selectByExampleWithBLOBs(siteSingleExample);
        //2.将mapper查询结果复制到pageInfo
        PageInfo<SiteSingle> pageInfo=new PageInfo<>(siteSingleList);
        //3.pageInfo计算处理处理=>后分页的总行数set到pageDto做记录
        pageDto.setTotalSize(pageInfo.getTotal());

        List<SiteSingleDto> siteSingleDtoList=new ArrayList<>();
        siteSingleList.stream().forEach(siteSingle->{
            SiteSingleDto siteSingleDto=new SiteSingleDto();
            BeanUtils.copyProperties(siteSingle,siteSingleDto);
            siteSingleDtoList.add(siteSingleDto);
        });

        pageDto.setListPageData(siteSingleDtoList);

    }

    //3.新增、修改siteSingle，将传入的id转为siteSingle对象本身
    public void save(SiteSingleDto siteSingleDto){
        SiteSingle siteSingle= DuplicateUtil.copy(siteSingleDto,SiteSingle.class);
        if(StringUtils.isEmpty(siteSingleDto.getUniId())){
            this.insert(siteSingle);
        }else{
            this.update(siteSingle);
        }

    }

    //4.删除模块
    public void delete(String uniId){
        SiteSingleExample siteSingleExample=new SiteSingleExample();
        siteSingleMapper.deleteByPrimaryKey(uniId);
    }

    //5.向外暴露dto，不暴露实体类：插入数据
    private void insert(SiteSingle siteSingle){
        siteSingle.setUniId(UUID8Util.getShortUUID());

        try {
        Date now =new Date();
        String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now);
        long time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date).getTime();
        int timeInt=(int)(time/1000);
        siteSingle.setCreateTime(now);
        siteSingle.setUpdateTime(now);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        siteSingleMapper.insertSelective(siteSingle);
    }

    //6.更新模块
    private void update(SiteSingle siteSingle){
        try {
            Date now =new Date();
            String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now);
            long time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date).getTime();
            int timeInt=(int)(time/1000);

            siteSingle.setUpdateTime(now);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        siteSingleMapper.updateByPrimaryKeySelective(siteSingle);
    }

}
